package top.codedance.iotp.client.plugins.async.opc;

import com.alibaba.fastjson.JSONArray;
import top.codedance.iotp.client.ClientApplication;
import top.codedance.iotp.common.executer.AsyncAbstractExecuter;
import top.codedance.iotp.common.constent.Command;
import top.codedance.iotp.common.entity.CommonEntity;

public abstract class Service {
    protected AsyncAbstractExecuter executer;
    protected OpcServiceParam param;

    public Service(AsyncAbstractExecuter executer, OpcServiceParam param){
        this.executer = executer;
        this.param = param;
    }

    /**
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public <T extends Service> T getInstance(Class<T> clazz){
        return (T)this;
    }

    /**
     * 创建连接
     * @throws Exception
     */
    public abstract void createConnect() throws Exception;

    /**
     * 定阅
     */
    public abstract void subscribe();

    /**
     * 关闭服务
     */
    public abstract void close();

    /**
     * 是否在运行
     * @return
     */
    public abstract boolean isRunning();

    /**
     * 发送信息给所有监听本主题的客户端
     * @param tag
     * @param body
     */
    public abstract void receipt(String tag, Object body);

    /**
     * 写入OPC数据
     * @param tag
     * @param ns
     * @param value
     */
    public abstract void send(String tag, int ns, Object value);

    /**
     * 写入OPC数据
     * @param writeArray
     */
    public abstract void send(JSONArray writeArray);

    /**
     * 统一获取发送实体
     * @return
     */
    protected CommonEntity genCommonEntity(){
        CommonEntity ce = new CommonEntity();
        ce.setCommand(Command.ASYNC_OPC_COMMAND_RECEIVE);
        ce.setVersion(ClientApplication.getDataVersion());
        return ce;
    }
}
